package HLCode.ASM.value;

import HLCode.HLCustomFunction;
import HLCode.HLCustomFunction_H;
import HLCode.HLObject;

public class ASM_Bool implements HLCustomFunction_H {
    byte type;

    boolean immediateValue;

    byte[] links;

    byte index;

    public int Load(byte[] data, int pos) {
        type = data[pos++];
        switch (type) {
        case OWNER_OBJECT_TYPE_IMMEDIATE_BOOL: {
            immediateValue = data[pos++] != 0;
            break;
        }
        default: {
            int levelCount = data[pos++];
            links = new byte[levelCount];
            System.arraycopy(data, pos, links, 0, levelCount);
            pos += levelCount;
            index = data[pos++];
        }
        }
        return pos;
    }

    public boolean GetValue(HLCustomFunction ownerFunction) {
        if (type == OWNER_OBJECT_TYPE_IMMEDIATE_BOOL) {
            return immediateValue;
        } else {
            HLObject valueOwnerObject = ownerFunction.ownerObjectGroup[type];
            for (int i = 0; i < links.length; i++) {
                valueOwnerObject = valueOwnerObject.GetObject(links[i]);
            }
            return valueOwnerObject.GetBoolean(index);
        }
    }
}
